#coding:UTF-8

import requests
import os
import time
from xpinyin import Pinyin
from lxml import etree
import json
import re

class Qianku:
    def __init__(self):
        self.headers = {
                           "Accept": "application/json, text/javascript, */*; q=0.01",
                           "X-Requested-With": "XMLHttpRequest",
                           "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
                           "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
                       }
        self.urls = ['http://588ku.com/sucai/0-default-0-0-guonian-0-1/']

    def create_dir(self, keyword):
        self.dir_name = '/home/syy/音乐/'+keyword
        if not os.path.exists(self.dir_name):
            os.mkdir(self.dir_name)
    def keyword_url(self):
        keyword = input("请输入关键字：")
        if keyword == '':
            os.exit()
        self.create_dir(keyword)
        pinyin = self.char_to_pinyin(keyword)

        for i in range(1,2):
            self.urls.append("http://588ku.com/sucai/0-default-0-0-{}-0-{}/".format(pinyin, i))

    def char_to_pinyin(self, char):
        word = Pinyin().get_pinyin(char)
        word = word.replace('-','')
        return word

    def parse_url(self, url):
        response = requests.get(url, headers = self.headers)
        return response

    def ana_data(self):
        for url in self.urls:
            response = self.parse_url(url)
            html = etree.HTML(response.content.decode('utf-8', 'ignore'))
            img_urls = html.xpath('//a[@class="img-box"]/@href')
#             img_urls = ['//588ku.com/ycpng/11770170.html', '//588ku.com/ycpng/12777345.html', '//588ku.com/ycpng/12802849.html', '//588ku.com/ycpng/12804082.html', '//588ku.com/ycpng/12610152.html', '//588ku.com/ycpng/12062172.html', '//588ku.com/ycpng/11644471.html', '//588ku.com/ycpng/11647048.html', '//588ku.com/ycpng/12801014.html', '//588ku.com/ycpng/12795518.html', '//588ku.com/ycpng/12786444.html', '//588ku.com/ycpng/12789481.html', '//588ku.com/ycpng/12610150.html', '//588ku.com/ycpng/12649141.html', '//588ku.com/ycpng/11647046.html', '//588ku.com/ycpng/11786030.html', '//588ku.com/ycpng/12773359.html', '//588ku.com/ycpng/11647034.html', '//588ku.com/ycpng/12777128.html', '//588ku.com/ycpng/11645556.html', '//588ku.com/ycpng/12785455.html', '//588ku.com/ycpng/12777342.html', '//588ku.com/ycpng/12801011.html', '//588ku.com/ycpng/11701376.html', '//588ku.com/ycpng/12780510.html', '//588ku.com/ycpng/11645277.html', '//588ku.com/ycpng/12783459.html', '//588ku.com/ycpng/12596758.html', '//588ku.com/ycpng/12786217.html', '//588ku.com/ycpng/12793687.html', '//588ku.com/ycpng/11695284.html', '//588ku.com/ycpng/12789463.html', '//588ku.com/ycpng/12786225.html', '//588ku.com/ycpng/10682864.html', '//588ku.com/ycpng/11695364.html', '//588ku.com/ycpng/12785454.html', '//588ku.com/ycpng/12649075.html', '//588ku.com/ycpng/12786226.html', '//588ku.com/ycpng/12785156.html', '//588ku.com/ycpng/11846515.html', '//588ku.com/ycpng/12781896.html', '//588ku.com/ycpng/12789382.html', '//588ku.com/ycpng/12771763.html', '//588ku.com/ycpng/12785157.html', '//588ku.com/ycpng/12649074.html']
            for img_url in img_urls:
                res = requests.get('http:'+img_url, headers = self.headers)
                html = etree.HTML(res.content.decode('utf-8', 'ignore'))
                src = 'http:' + html.xpath("//div[@class='img-box']/img/@src")[0]
                name = html.xpath("//div[@class='img-box']/img//@title")[0]
                self.save(src, name)

    def save(self, src, name):
        response = self.parse_url(src)
        if response.status_code == 200:
            print('正在下载图片：' + name + "_" + src)
            down_img_name = self.dir_name+"/"+name+'_{}.jpg'.format(int(time.time()))
            f= open(down_img_name, "wb")
            f.write(response.content)

if __name__ == '__main__':
    qianku = Qianku()
    urls = qianku.keyword_url()
    qianku.ana_data()
